/**
 * 用户管理
 */
var User = {};

/**
 * 载入用户列表页面
 */
User.loadUserList = function(){
//	$.post( Common.path + "/sysmanage/user/userlist",function(html){
//		$("#main_right").html(html);
//	});
	$.pjax({
		url: Common.path + "/sysmanage/user/userlist",
		container: '#main_right'
	});
};

/**
 * 新增用户
 */
User.openNewUser = function(){
	$("#dialog_unique1_cont").html("");
	$("#dialog_unique1_cont").load( Common.path+"/sysmanage/user/user_pre_save",{"username":""} );
	$("#dialog_unique1").show();
	$("#dialog_unique1").dialog({
		title:"新增角色",width:"290",height:"300", modal:true, 
		buttons:[
		{
			text:"保存",  iconCls:'icon-ok',
			handler:function(){
				var btn = $(this);
				var f = $("form[name='user_add_form']");
				if( f.form("validate") == false ){
					return ;
				}
			    var op ={
			    	url: Common.path + "/sysmanage/user/user_save",
			        type:'POST', dataType: 'json', 
			        beforeSubmit:function(){
			        	btn.linkbutton("disable");
			        },
			        success: function(json) {
			        	if( json.res = 1 ){
			        		$("#dialog_unique1").dialog("close");
			        		$("#user_datagrid").datagrid('reload');
			        	}
			        	btn.linkbutton("enable");
			        },error:function(){
			        	btn.linkbutton("enable");
			        }
			    };
		    	f.ajaxSubmit( op ); 
			}
		} , 
		{
			text:"取消",  iconCls:'icon-cancel',
			handler:function(){
				$("#dialog_unique1").dialog("close");
			}
		}
		]
	});
};

/**
 * 打开修改用户页面
 */
User.openEditUser = function( index, username ){
	$("#user_datagrid").datagrid('showExtendContent_x',index);
	var div = $("#user_datagrid").datagrid('findExtendContent_x',index);
	$.post(Common.path+"/sysmanage/user/user_pre_save",{"username":username},
		function(html){
			 div.html(html);
			 $("#user_datagrid").datagrid('resizeExtendContent_x');
		}
	);
};

User.editUser = function( that ){
	var btn = $(that);
	var f = $("form[name='user_edit_form']");
    var op ={
    	url: Common.path + "/sysmanage/user/user_save",
        type:'POST', dataType: 'json', 
        beforeSubmit:function(){
        	btn.linkbutton("disable");
        },
        success: function(json) {
        	if( json.res == 1){
        		$.messager.alert("系统提示", "保存成功", 'info' );
        	}else if( json.res == -1){
        		$.messager.alert("系统提示", "没有权限", 'warning' );
        	}
        	btn.linkbutton("enable");
        },error:function(){
        	btn.linkbutton("enable");
        }
    };
	f.ajaxSubmit( op ); 
};


/**
 * 隐藏User详情页面
 */
User.closeEditUser = function( that ){
	var i = $(that).closest("div[datagrid-row-extendcont-index]").attr("datagrid-row-extendcont-index");
	$("#user_datagrid").datagrid("hideExtendContent_x", i);
	$("#user_datagrid").datagrid('resizeExtendContent_x');
};

/**
 * 打开分配角色页面
 */
User.openAssignRole = function( index, username ){
	$("#dialog_unique1_cont").html("");
	$("#dialog_unique1_cont").load( Common.path+"/sysmanage/user/user_pre_assignRole",{"username":username, "index":index } );
	$("#dialog_unique1").show();
	$("#dialog_unique1").dialog({
		title:"分配角色",width:"200",height:"300", modal:true, 
		buttons:[
		{
			text:"保存",  iconCls:'icon-ok',
			handler:function(){
				var btn = $(this);
				var f = $("form[name='assign_grouprole_form']");
				var nodes = $("#assign_grouprole_tree").tree("getChecked");
				var ids = jQuery.map( nodes, function( node ){
					return node.id;
				});
				f.find("[name='roleIds']").val(ids);
			    var op ={
			        type:'POST', dataType: 'json', 
			        beforeSubmit:function(){
			        	btn.linkbutton("disable");
			        },
			        success: function(json) {
			        	$("#dialog_unique1").dialog("close");
			        	btn.linkbutton("enable");
			        },error:function(){
			        	btn.linkbutton("enable");
			        }
			    };
		    	f.ajaxSubmit( op ); 
			}
		} , 
		{
			text:"取消",  iconCls:'icon-cancel',
			handler:function(){
				$("#dialog_unique1").dialog("close");
			}
		}
		]
	});
};

User.deleteUser = function( username ){
	$.messager.confirm( '确认框','确定删除?', function( r ){
		if( r ){
			$.getJSON( Common.path + "/sysmanage/user/user_delete",{"username":username}, 
					function( json ){
						if(json.res =1){
							$.messager.alert("系统提示", "删除成功" );
							$("#user_datagrid").datagrid('reload');
						}
					}
			);
		}
	});
	
};
